package com.yuexun.dhdoctor.module.main.ui;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.AppContext;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.base.ExitApplication;
import com.yuexun.dhdoctor.baselistener.ICallBackListener;
import com.yuexun.dhdoctor.module.Contract.ui.Frag_Contract;
import com.yuexun.dhdoctor.module.Government.Frag_Business;
import com.yuexun.dhdoctor.module.Government.Frag_Government;
import com.yuexun.dhdoctor.module.my.ui.Frag_My;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Act_Main extends BaseKJActivity {

    @Bind(R.id.main_content)
    FrameLayout mainContent;
    @Bind(R.id.visit_icon)
    ImageView visitIcon;
    @Bind(R.id.visit_txt)
    TextView visitTxt;
    @Bind(R.id.visit)
    LinearLayout visit;
    @Bind(R.id.visit_circle)
    ImageView visitCircle;
    @Bind(R.id.doctor_icon)
    ImageView doctorIcon;
    @Bind(R.id.doctor_txt)
    TextView doctorTxt;
    @Bind(R.id.doctor)
    LinearLayout doctor;
    @Bind(R.id.doctor_circle)
    ImageView doctorCircle;
    @Bind(R.id.health_icon)
    ImageView healthIcon;
    @Bind(R.id.health_txt)
    TextView healthTxt;
    @Bind(R.id.health)
    LinearLayout health;
    @Bind(R.id.health_circle)
    ImageView healthCircle;
    @Bind(R.id.my_icon)
    ImageView myIcon;
    @Bind(R.id.my_txt)
    TextView myTxt;
    @Bind(R.id.my)
    LinearLayout my;
    @Bind(R.id.my_circle)
    ImageView myCircle;

    Frag_Government fragment_government;
    Frag_Business fragment_businsee;
    Frag_Contract fragContract;
    Frag_My fragMy;

    ICallBackListener clickListener;

    public void setClickListener(ICallBackListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.act_main);
        ButterKnife.bind(this);
        ExitApplication.getInstance().addActivity(this);

        initDatas();
    }

    public void initDatas() {
        fragment_government = new Frag_Government();
        fragment_businsee = new Frag_Business();
        fragContract = new Frag_Contract();
        fragMy = new Frag_My();

        fragMy.setBackListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {
                if (bundle != null) {
                    int count = bundle.getInt("count");
                    if (count > 0) {
                        myCircle.setVisibility(View.VISIBLE);
                    } else {
                        myCircle.setVisibility(View.GONE);
                    }
                }
            }

        });
        changeFragment(R.id.main_content, fragment_government);
    }

    @OnClick({R.id.bottombar_home, R.id.bottombar_doctor, R.id.bottombar_health, R.id.bottombar_my})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bottombar_home:
                visitIcon.setImageResource(R.drawable.ic_home_selected);
                doctorIcon.setImageResource(R.drawable.ic_business);
                healthIcon.setImageResource(R.drawable.ic_maillist);
                myIcon.setImageResource(R.drawable.ic_my);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                changeFragment(R.id.main_content, fragment_government);
                break;
            case R.id.bottombar_doctor:
                visitIcon.setImageResource(R.drawable.ic_home);
                doctorIcon.setImageResource(R.drawable.ic_business_selected);
                healthIcon.setImageResource(R.drawable.ic_maillist);
                myIcon.setImageResource(R.drawable.ic_my);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                changeFragment(R.id.main_content, fragment_businsee);
                break;
            case R.id.bottombar_health:
                visitIcon.setImageResource(R.drawable.ic_home);
                doctorIcon.setImageResource(R.drawable.ic_business);
                healthIcon.setImageResource(R.drawable.ic_maillist_selected);
                myIcon.setImageResource(R.drawable.ic_my);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                changeFragment(R.id.main_content, fragContract);
                break;
            case R.id.bottombar_my:
                visitIcon.setImageResource(R.drawable.ic_home);
                doctorIcon.setImageResource(R.drawable.ic_business);
                healthIcon.setImageResource(R.drawable.ic_maillist);
                myIcon.setImageResource(R.drawable.ic_my_selected);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                changeFragment(R.id.main_content, fragMy);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // menuUtils.createTwoDispatcher(event);
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
//            Intent intent = new Intent();
//            intent.setAction("android.intent.action.MAIN");
//            intent.addCategory("android.intent.category.HOME");
//            startActivity(intent);
            //返回键最小化应用
            ((AppContext) getApplication()).setIsLocked(true);
            moveTaskToBack(isFinishing());
        }
        return false;
    }
}
